﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI.WebControls;
using System.Globalization;
using Telerik.Web.UI;

namespace RealEstate.Common
{
    public class Utilities
    {
        public static int GetIndexByValue(RadComboBox ddl, string value)
        {
            int i = 0;
            foreach (RadComboBoxItem item in ddl.Items)
            {
                if (item.Value.Equals(value))
                    return i;
                i++;
            }

            return i;
        }

        public static int GetIndexByText(RadComboBox ddl, string text)
        {
            int i = 0;
            foreach (RadComboBoxItem item in ddl.Items)
            {
                if (item.Text.Equals(text))
                    return i;
                i++;
            }

            return i;
        }

        public static string ConvertCurrency(decimal? value)
        {
            return value.Value.ToString("C0", CultureInfo.CreateSpecificCulture("en-US"));
        }

        public static string ConvertDateTime(DateTime? dt)
        {
            return string.Concat(dt.Value.Month, "/", dt.Value.Day, "/", dt.Value.Year);
        }

        public static void LoadClientForDropDownlist(RadComboBox ddl)
        {
            ClientEntities entity = new ClientEntities();
            ddl.DataSource = entity.GetAll();
            ddl.DataTextField = "FullName";
            ddl.DataValueField = "ClientId";
            ddl.DataBind();
            RadComboBoxItem item = new RadComboBoxItem(string.Empty);
            ddl.Items.Insert(0, item);
        }

        public static void LoadPropertyForDropDownlist(RadComboBox ddl)
        {
            PropertyEntities entity = new PropertyEntities();
            ddl.DataSource = entity.GetAll();
            ddl.DataTextField = "SubjectProperty";
            ddl.DataValueField = "PropertyId";
            ddl.DataBind();
            RadComboBoxItem item = new RadComboBoxItem(string.Empty);
            ddl.Items.Insert(0, item);
        }

        public static void LoadPropertyTypeForDropDownlist(RadComboBox ddl)
        {
            PropertyTypeEntities entity = new PropertyTypeEntities();
            ddl.DataSource = entity.GetAll();
            ddl.DataTextField = "PropertyType1";
            ddl.DataValueField = "Id";
            ddl.DataBind();
            RadComboBoxItem item = new RadComboBoxItem(string.Empty);
            ddl.Items.Insert(0, item);
        }

        public static void LoadClientTypeForDropDownlist(RadComboBox ddl)
        {
            ClientTypeEntities entity = new ClientTypeEntities();
            ddl.DataSource = entity.GetAll();
            ddl.DataTextField = "ClientType1";
            ddl.DataValueField = "Id";
            ddl.DataBind();
            RadComboBoxItem item = new RadComboBoxItem(string.Empty);
            ddl.Items.Insert(0, item);
        }

        public static void LoadSourceOfBusinessForDropDownlist(RadComboBox ddl)
        {
            SourceOfBusinessEntities entity = new SourceOfBusinessEntities();
            ddl.DataSource = entity.GetAll();
            ddl.DataTextField = "SourceOfBusiness1";
            ddl.DataValueField = "Id";
            ddl.DataBind();
            RadComboBoxItem item = new RadComboBoxItem(string.Empty);
            ddl.Items.Insert(0, item);
        }

        public static void LoadUrgencyForDropDownlist(RadComboBox ddl)
        {
            UrgencyEntities entity = new UrgencyEntities();
            ddl.DataSource = entity.GetAll();
            ddl.DataTextField = "Urgency1";
            ddl.DataValueField = "Id";
            ddl.DataBind();
            RadComboBoxItem item = new RadComboBoxItem(string.Empty);
            ddl.Items.Insert(0, item);
        }

        public static void LoadStateForDropDownlist(RadComboBox ddl)
        {
            StateEntities entity = new StateEntities();
            ddl.DataSource = entity.GetAll();
            ddl.DataTextField = "State1";
            ddl.DataValueField = "Id";
            ddl.DataBind();
            RadComboBoxItem item = new RadComboBoxItem(string.Empty);
            ddl.Items.Insert(0, item);
        }

        public static void LoadCityForDropDownlist(RadComboBox ddl)
        {
            CityEntities entity = new CityEntities();
            ddl.DataSource = entity.GetAll();
            ddl.DataTextField = "City1";
            ddl.DataValueField = "Id";
            ddl.DataBind();
            RadComboBoxItem item = new RadComboBoxItem(string.Empty);
            ddl.Items.Insert(0, item);
        }

        public static void LoadStatusForDropDownlist(RadComboBox ddl)
        {
            StatusEntities entity = new StatusEntities();
            ddl.DataSource = entity.GetAll();
            ddl.DataTextField = "Status1";
            ddl.DataValueField = "Id";
            ddl.DataBind();
            RadComboBoxItem item = new RadComboBoxItem(string.Empty);
            ddl.Items.Insert(0, item);
        }

        public static string GetViewName(int viewId)
        {
            switch (viewId.ToString())
            {
                case Constants.ViewByAccountingId:
                    return "~/Views/Loan/ByAccounting.aspx";
                case Constants.ViewByCoLastNameId:
                    return "~/Views/Transaction/ByCoLastName.aspx";
                case Constants.ViewByREStatusId:
                    return "~/Views/Transaction/ByREStatus.aspx";
                case Constants.ViewByHistoryId:
                    return "~/Views/Loan/ByAccounting.aspx";
                case Constants.ViewByLastNameId:
                    return "~/Views/Transaction/ByLastName.aspx";
                case Constants.ViewByLoanStatusId:
                    return "~/Views/Loan/ByLoanStatus.aspx";
                case Constants.ViewByStreetNameId:
                    return "~/Views/Transaction/ByStreetName.aspx";
            }

            return "~/Views/Transaction/ByREStatus.aspx";
        }
    }
}